home *** CD-ROM | disk | FTP | other *** search
- Path: wkaufman.us.oracle.com!wkaufman
- From: wkaufman@wkaufman.us.oracle.com (William Kaufman)
- Newsgroups: comp.lang.c
- Subject: Re: I/O Question
- Date: 20 Jan 1996 16:11:08 GMT
- Organization: Oracle Corporation, Redwood Shores CA
- Message-ID: <4dr46s$sab@inet-nntp-gw-1.us.oracle.com>
- References: <4dp1nm$mu6@taco.cc.ncsu.edu>
- NNTP-Posting-Host: wkaufman.us.oracle.com
-
- In article <4dp1nm$mu6@taco.cc.ncsu.edu> Alex David Groce <adgroce> writes:
- ]
- ] while (scanf("%i", &list[count++]) != EOF); // Read elements.
- ] count--; // Avoid good old OBOB, the Off-By-One-Bug.
- ]
- ] Would it be worth my time to learn buffered I/O techniques, read or
- ] getchar or such, to input this data? Every little bit of time
- ] counts... Or is there a good example somewhere of a fast way to do
- ] this?
-
- The <stdio.h> functions are already buffered. There's probably not
- much you can do on that end to speed this up. A couple of ideas,
- though:
-
- a) Change the program to use a file (and fscanf()) instead of stdin.
- On some implementations, stdin may be effectively buffered one line at a
- time; but files are usually buffered by about 2Kb (or whatever BUFSIZ is
- #define'd as in <stdio.h>).
-
- b) If you've got a lot of memory to throw at the problem, you could
- try using setbuf() or setvbuf() (esp. with a static array) to increase
- the buffer size (but that might actually slow down your program if it
- starts paging,...).
-
- c) Calling fgets() to get a line and strtol() to parse it may be
- faster than using *scanf() functions, since scanf() does a lot more than
- just parse numbers.
-
- And, the number one response to "How can I make this faster":
-
- d) Use a profiling tool, find out where the time is actually going,
- and concentrate on those parts of your program. Most compilers come
- with such a tool: on Unix, check the man pages for prof(1) and gprof(1),
- and for your compiler to see if it's got an option like "-p" or "-pg";
- or, if you're horribly under-budget, I'd heartily recommend "Quantify"
- from Pure Software.
-
- -- Bill K.
-
- Bill Kaufman, | ``Can anyone tell me what happened to my
- wkaufman@us.oracle.com | "Hello" and why?'' -- David Sheffield
-